const fs = require("fs");
const path = require("path");
const chalk = require("chalk");
const asking = require("./asking");
const checkAndMkDir = require("./utils/checkAndMkDir");

const genDir = (name) => {
  const exist = fs.existsSync(name);
  if (!exist) fs.mkdirSync(name);
};

async function dir(args, number) {
  const fpath = args[0];
  let prefix;
  let inputNum;

  if (!fpath) {
    // 获取必要信息
    const answers = await asking("dir", number);
    prefix = answers.prefix;
    inputNum = answers.inputNum;
  }

  // if (!fpath) return console.log(chalk.yellow("请输入文件名"));

  const filePath = prefix || fpath;
  const fileCount = number || inputNum;

  checkAndMkDir(filePath);

  if (fileCount && fileCount > 1) {
    for (let i = 0; i < fileCount; i++) {
      genDir(`${filePath}${i}`);
    }
  } else {
    genDir(filePath);
  }

  console.log(chalk.green("创建完成"));
}

module.exports = dir;
